﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
//using OL.FrameCore.Domain.IRepository;
using OL.FrameCore.Infrastructure.Utility;
using Microsoft.EntityFrameworkCore;

namespace OL.FrameCore.Application
{
    public class ServiceBase<TDto, TEntity> : IServiceBase<TDto> where TDto : class where TEntity : class
    {
        IRepositoryBase<TEntity> _repository;
        public ServiceBase(IRepositoryBase<TEntity> repository)
        {
            _repository = repository;
        }

        public List<TDto> GetList()
        {
            var list = _repository.GetAllList();
            List<TDto> listResult = new List<TDto>();
            foreach (var li in list)
            {
                var model = EntityMap.Map<TEntity, TDto>(li);//对象属性映射
                listResult.Add(model);
            }
            return listResult;
        }

        public TDto GetModel(long id)
        {
            //var model = _repository.GetModel(id);
            //var result = EntityMap.Map<TEntity, TDto>(model);
            //return result;

            return null;
        }

        public bool Insert(TDto dto)
        {
            var entity = EntityMap.Map<TDto, TEntity>(dto);
            return _repository.Insert(entity);
        }

        public bool Update(TDto dto)
        {
            var entity = EntityMap.Map<TDto, TEntity>(dto);
            return _repository.Update(entity);
        }

        public bool Delete(long id)
        {
            //return _repository.Delete(id);
            return false;
        }

        public bool SoftDelete(long id)
        {
            //return _repository.SoftDelete(id);
            return false;
        }

        public T2 ToMap<T2>(object obj)
        {
            return EntityMap.Map<T2>(obj);
        }
    }
}
